/**
 * usableFields plugin (v0.6), require jQuery 1.3+
 * Copyright (c) 2010 David Lafon, http://web-intention.com/
 * Licensed under the MIT license http://opensource.org/licenses/mit-license.php
 */

(function($) {
 
  $.fn.usableFields = function(settings) {
    var config =  { display: 'label',                                               // type of content to display in field 'title', 'label' or jQuery selector
                    hideLabel: true,                                                // show or hide the label linked with field
          					blurClass: '',                                                  // CSS class applied to blured fields
          					focusClass: '',                                                 // CSS class applied to focused fields (for IE6/7 ":focus" deficiencies)
                    fields: 'input[type=text], input[type=password], textarea'      // jQuery selectors for selected fields to be impacted by plugin
                  };
 
    if (settings) settings = $.extend(config, settings);
    
    this.each(function() {
      
      switch (settings.display) {
        /**
         * display title content in field
         */
        case 'title':
          
          jQuery((settings.fields), $(this)).each(function(){
            if (settings.blurClass && !$(this).val()) $(this).addClass(settings.blurClass);
            /**
             * hide label if defined
             */
            if (settings.hideLabel){
              $label = getLabel($(this));
              $(this).prev('label').hide();
            }
            
            defaultContent = $(this).attr('title');
            if (!$(this).val()) $(this).val(defaultContent);
            
            $(this).focus(function(){
              onFocus($(this).attr('title'), $(this), settings);
        		});
            
            $(this).blur(function(){
              onBlur($(this).attr('title'), $(this), settings);
        		});
          });
          break;
        
        /**
         * display label content in field
         */
        case 'label':
          jQuery((settings.fields), $(this)).each(function(){
            if (settings.blurClass && !$(this).val()) $(this).addClass(settings.blurClass);
            
            $label = getLabel($(this));
            /**
             * hide label if defined
             */
            if (settings.hideLabel){
              $label.hide();
            }
            if (!$(this).val()) $(this).val($label.text());

              
            $(this).focus(function(){
              $currentLabel = getLabel($(this));
              onFocus($currentLabel.text(), $(this), settings);
        		});
            
            $(this).blur(function(){
              $currentLabel = getLabel($(this));
              onBlur($currentLabel.text(), $(this), settings);
        		});  
          });
          break;  
        
        /**
         * display dom node content in field
         */
        default:
          $((settings.display), $(this)).hide();
          jQuery((settings.fields), $(this)).each(function(){
            if (settings.blurClass && !$(this).val()) $(this).addClass(settings.blurClass);
            
            /**
             * hide label if defined
             */
            if (settings.hideLabel){
              $label = getLabel($(this));
              $label.hide();
            }
            
            if (!$(this).val()) $(this).val($(this).next(settings.display).text());
              
            $(this).focus(function(){
              onFocus($(this).next(settings.display).text(), $(this), settings);
        		});
            
            $(this).blur(function(){
              onBlur($(this).next(settings.display).text(), $(this), settings);
        		});  
          });
          break;
      }
    });
     
    /**
     * chain
     */
    return this;
  };
  
  /**
   * when the field is focusing
   */
  function onFocus(defaultContent, elt, settings) {
    if (elt.val() == defaultContent) elt.val('');
    if (settings.focusClass) elt.addClass(settings.focusClass);
		if (settings.blurClass) elt.removeClass(settings.blurClass);
  };
  
  /**
   * when the field is bluring
   */
  function onBlur(defaultContent, elt, settings){
    if ( elt.val() == defaultContent || jQuery.trim(elt.val()) == '' ){
			elt.val(defaultContent);
      if (settings.blurClass) elt.addClass(settings.blurClass);
		}
		if (settings.focusClass) elt.removeClass(settings.focusClass);
  };
  
  /**
   * return the label linked to field
   */
  function getLabel(elt) {
    $id = elt.attr('id');
    if ($label = elt.prev('label[for='+$id+']')) {
      return $label;
    }
    else {
      return elt.next('label[for='+$id+']');
    }
  };
 
})(jQuery);


